$(function () {
    findBusiness();
})

var business = {
    "businessCategory.id": "",
    page: 1,
    limit: 9
};


// 根据传入的分类id,页码和每页条数查询业务。
function findBusiness() {
    layui.use(['laypage', 'layer'], function () {
        var laypage = layui.laypage
            , layer = layui.layer;
        let bcid = sessionStorage.getItem("businessCategoryid");
        var businessRes;
        /*        setLocationNow(res.businessCategory);
                setBusinessCategory(res.businessCategory)*/
        if (bcid != null) {
            business["businessCategory.id"] = bcid;  // 获取传入的分类id
            sessionStorage.removeItem("businessCategoryid");
        }
        businessRes = myAjax(ipAddress()+"/business/g", business, "get", false);
        //可以传分类id也可以不传,不传默认查出所有business
        let businessCateData = myAjax(ipAddress()+"/business/category/g", {}, "get", false);
        setBusinessCategory(businessCateData.data);
        setLocationNow(businessCateData.data);
        laypage.render({
            elem: 'page'
            , count: businessRes.count //数据总数
            , limit: 9
            , jump: function (obj) { //obj中obj.curr是当前页码,obj.limit是每一页包含的数据数目.
                business.page = obj.curr;
                business.limit = obj.limit;
                let res = myAjax(ipAddress()+"/business/g", business, "get", false);
                if (res != null) {
                    setBusiness(res.data);
                }
            }
        });
    });

}

// 设置业务范围右边的导航栏
function setBusinessCategory(data) {
    let html = '<div>业务范围</div>';
    for (let i = 0; i < data.length; i++) {
        html += '<div onclick="findbyBusinessCategory(' + data[i].id + ')">' + data[i].name + '</div>';
    }
    html += '<div><img src="../img/zs_2.jpg"></div>';
    $("#right-navigate").html(html);
}

// 点击分类后传入id并查询数据
function findbyBusinessCategory(bcId) {
    business["businessCategory.id"] = bcId;
    findBusiness();
}

//点击分类后增加当前位置的小导航
function setLocationNow(data) {
    let html = '您的当前位置 <a href="/html/index.html">网站首页</a> ≡ <a href="/html/business.html">业务范围</a> ';
    // let id=sessionStorage.getItem("businessCategoryId");
    let id = business["businessCategory.id"];
    if (id == undefined || id == null) {
        html += '';
    } else {
        for (let i = 0; i < data.length; i++) {
            if (data[i].id == id) {
                html += '<a style="cursor: pointer;" onclick="findbyBusinessCategory(' + data[i].id + ')">  ≡ ' + data[i].name + '</a>';
            }
        }
    }
    $("#location").html(html);
}


// 设置业务数据
function setBusiness(data) {

    let html = '';

    for (let i = 0; i < data.length; i++) {
        html += ' <div onclick="findBusinessDetail(' + data[i].id + ')">\n' +
            '                    <img src="' + data[i].pic + '"/>\n' +
            '                    <div>' + data[i].name + '</div>\n' +
            '                </div>';
    }
    $("#business").html(html);
}


// 根据id查询具体的业务
function findBusinessDetail(id) {
    sessionStorage.setItem("businessDId", id);
    window.location.href = "/html/business-detail.html";
}